package com.ruiysoft.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruiysoft.controller.sys.api.InnerMicroprogramApi;
import com.ruiysoft.controller.sys.dto.CalculationDto;
import com.ruiysoft.controller.sys.res.CalculationResponse;
import com.ruiysoft.controller.sys.res.UserShopResponse;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.entity.sys.SysShopDistribution;
import com.ruiysoft.entity.sys.SysUser;
import com.ruiysoft.entity.sys.SysUserShopRelation;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.sys.ISysMicroShopService;
import com.ruiysoft.service.sys.ISysShopDistributionService;
import com.ruiysoft.service.sys.ISysUserService;
import com.ruiysoft.service.sys.ISysUserShopRelationService;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 化增光 on 2018/12/1.
 */
@RestController
public class InnerMicroprogramApiController implements InnerMicroprogramApi {

    private static final Logger log = LoggerFactory.getLogger(MicroprogramApiController.class);

    private final ObjectMapper objectMapper;

    @Resource
    ISysShopDistributionService iSysShopDistributionService;

//    @Resource
//    ISysShopPersonnelRelationService iSysShopPersonnelRelationService;

    @Resource
    ISysMicroShopService iSysMicroShopService;

    @Resource
    ISysUserShopRelationService iSysUserShopRelationService;

    @Resource
    ISysUserService iSysUserService;

    @org.springframework.beans.factory.annotation.Autowired
    public InnerMicroprogramApiController(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;

    }

    @Override
    public Result<List<CalculationResponse>> calculation(@ApiParam(value = "", required = true) @RequestBody @Valid CalculationDto calculationDto) {
        List<CalculationResponse> responses = new ArrayList<>();
        try {
            SysMicroShop sysMicroShop = iSysMicroShopService.selectSysMicroShopById(calculationDto.getShopId());
            Assert.notNull(sysMicroShop,"没有获取到店铺信息");
            SysShopDistribution distribution = iSysShopDistributionService.getOne(new QueryWrapper<SysShopDistribution>().eq("shop_id",calculationDto.getShopId()).le("create_time",calculationDto.getOrderTime()).orderBy(false,false,"create_time").last("limit 1"));
            if (null == distribution){
                return ResultGenerator.genSuccessResult(responses);
            }
            responses = iSysMicroShopService.calculation(distribution,calculationDto,responses);
            return ResultGenerator.genSuccessResult(responses);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result getShopkeeper(Long shopId) {

        try {
            SysMicroShop shop = iSysMicroShopService.selectSysMicroShopById(shopId);
            SysUserShopRelation relation = iSysUserShopRelationService.getOne(new QueryWrapper<SysUserShopRelation>().eq("shop_id",shopId));
            SysUser user = iSysUserService.getById(relation.getUserId());
            UserShopResponse response = new UserShopResponse()
                    .setId(shop.getId())
                    .setShopName(shop.getShopName())
                    .setShopImg(shop.getShopImg())
                    .setRealName(user.getRealName())
                    .setMobile(user.getMobile());
            return ResultGenerator.genSuccessResult(response);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result getShopInfos(Long... ids) {
        try {
            List<UserShopResponse> responses = iSysUserShopRelationService.selectUserShopInfoByIds(ids);
            return ResultGenerator.genSuccessResult(responses);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }
}
